import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-11-04
 * Time: 22:16
 */
public class Solution {
    // 二分查找
    public int maxDistance(int[] position, int m) {
        int len = position.length;
        Arrays.sort(position);
        int start = position[0];
        int end = position[len-1];
        // 最大值
        int right = end - start;
        int left = 1;
        while (left < right) {
            int mid = ((right-left+1) >> 1) + left;
            if (!judge(mid, position, m)) {
                right = mid - 1;
            } else {
                left = mid;
            }
        }
        return right;
    }


    /**
     *  二分查找
     */
    public boolean judge(int strength, int[] position, int m) {
        int count = 0;
        int pos = position[0];
        for (int i = 0; i < position.length; i++) {
            if (position[i] >= pos) {
                count++;
                pos =position[i] + strength;
                if (count >= m) {
                    return true;
                }
            }
        }
        return false;
    }
}
